;(function ($){
    //  coupon 位
    $('#detailCoupon').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/coupon/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#update_Coupon_form input[name="username"]').val(data.username);
                $('#update_Coupon_form input[name="surename"]').val(data.surename);
                $('#update_Coupon_form input[name="primary_key"]').val(data.id);
                $('#update_Coupon_form input[name="uid"]').val(data.id);
                $('#update_Coupon_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    //  ProductProperty 位
    $('#detailProductProperty').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/product_property/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#update_product_property_form input[name="product_name"]').val(data.product.name);
                $('#update_product_property_form input[name="name"]').val(data.name);
                $('#update_product_property_form input[name="detail"]').val(data.detail);
                $('#update_product_property_form input[name="primary_key"]').val(data.id);
                $('#update_product_property_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // Banner位
    $('#detailBanner').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/banner/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#update_banner_form input[name="name"]').val(data.name);
                $('#update_banner_form input[name="description"]').val(data.description);
                $('#update_banner_form input[name="primary_key"]').val(data.id);
                $('#update_banner_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // BannerItem
    $('#detailBannerItem').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: 'banner_item/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                (data.type == 1) ?  $('#update_banner_item_form select option[value="1"]').attr('selected', 'selected') :  $('#update_banner_item_form select option[value="2"]').attr('selected', 'selected');
                $('#update_banner_item_form input[name="key_word"]').val(data.key_word);
                $('#update_banner_item_form input[name="alias"]').val(data.img.alias);
                $('#update_banner_item_form input[name="url"]').val(data.img.url);
                $('#update_banner_item_form img').attr('src', data.img.url);
                $('#update_banner_item_form input[name="status"][value="' + data.status + '"]').iCheck('check');
                $('#update_banner_item_form input[name="primary_key"]').val(data.id);
                $('#update_banner_item_form input[name="image_primary_key"]').val(data.img.id);
            }
        });
    });

    // detailBannerItem 填充
    $('#detailBannerItem').on('show.bs.modal', function(e) {
        $('#detailBannerItem select[name="banner_id"]').empty();
        $.ajax({
            url: '/banner_item/fill',
            type: 'get',
            success: function(data) {
                var rows = data.data;
                rows.forEach(function (item) {
                    var op = '<option value="' + item.id + '">'+ item.name + '</option>';
                    $('#detailBannerItem select[name="banner_id"]').append(op);
                })
            }
        });
    });

    // addBannerItem 填充
    $('#addBannerItem').on('show.bs.modal', function(e) {
        $('#addBannerItem select[name="banner_id"]').empty();
        $.ajax({
            url: '/banner_item/fill',
            type: 'get',
            success: function(data) {
                var rows = data.data;
                rows.forEach(function (item) {
                    var op = '<option value="' + item.id + '">'+ item.name + '</option>';
                    $('#addBannerItem select[name="banner_id"]').append(op);
                })
            }
        });
    });

    // Banner位 detailImage
    $('#detailImage').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/image/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#update_image_form input[name="alias"]').val(data.alias);
                $('#update_image_form input[name="url"]').val(data.url);
                $('#update_image_form input[name="primary_key"]').val(data.id);
                $('#update_image_form img').attr('src', data.url);
                $('#update_image_form select option').removeAttr('selected');
                (data.from == '本地') ?  $('#update_image_form select option[value="1"]').attr('selected', 'selected') :  $('#update_image_form select option[value="2"]').attr('selected', 'selected');
                $('#update_image_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // Theme位 detailTheme
    $('#detailTheme').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/theme/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#update_theme_form input[name="primary_key"]').val(data.id);
                $('#update_theme_form input[name="topic_img_key"]').val(data.topic_img.id);
                $('#update_theme_form input[name="head_img_key"]').val(data.head_img.id);
                $('#update_theme_form input[name="name"]').val(data.name);
                $('#update_theme_form input[name="cpp_hot"]').val(data.cpp_hot);
                $('#update_theme_form input[name="description"]').val(data.description);
                $('#update_theme_form input[name="topic_image_name"]').val(data.topic_img.alias);
                $('#update_theme_form input[name="topic_image_url"]').val(data.topic_img.url);
                $('#update_theme_form img[name="topic_img"]').attr('src', data.topic_img.url);
                $('#update_theme_form input[name="head_image_name"]').val(data.head_img.alias);
                $('#update_theme_form input[name="head_image_url"]').val(data.head_img.url);
                $('#update_theme_form img[name="head_img"]').attr('src', data.head_img.url);
                $('#update_theme_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // ProductImage
    $('#detailProductImage').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/product_image/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#update_product_image_form input[name="product_name"]').val(data.product.name);
                $('#update_product_image_form input[name="alias"]').val(data.img.alias);
                $('#update_product_image_form input[name="order"]').val(data.order);
                $('#update_product_image_form input[name="description"]').val(data.description);
                if(data.img.from == '本地') {
                    $('#update_product_image_form input[name="product_bottom_image"]').val(data.img.url.substr(data.img.url.lastIndexOf('/') + 1));
                } else {
                    $('#update_product_image_form input[name="product_bottom_image"]').val(data.img.url);
                }
                $('#update_product_image_form img').attr('src', data.img.url);
                $('#update_product_image_form input[name="primary_key"]').val(data.id);
                $('#update_product_image_form img').attr('src', data.url);
                $('#update_product_image_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // ProductInfo
    $('#detailProductInfo').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/product_info/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#update_product_info_form input[name="name"]').val(data.product.name);
                $('#update_product_info_form input[name="description"]').val(data.description);
                $('#update_product_info_form input[name="alias"]').val(data.img.alias);
                if(data.img.from == '本地') {
                    $('#update_product_info_form input[name="url"]').val(data.url.substr(data.url.lastIndexOf('/') + 1));
                } else {
                    $('#update_product_info_form input[name="url"]').val(data.url);
                }
                $('#update_product_info_form input[name="price"]').val(data.price);
                $('#update_product_info_form input[name="member_price"]').val(data.member_price);
                $('#update_product_info_form input[name="purchase_price"]').val(data.purchase_price);
                $('#update_product_info_form input[name="stock"]').val(data.stock);
                $('#update_product_info_form input[name="color"]').val(data.color);
                 $('#update_product_info_form input[name="wtcdtp"]').val(data.wtcdtp);
                $('#update_product_info_form input[name="hmkcbc"]').val(data.hmkcbc);
                $('#update_product_info_form input[name="floorprice"]').val(data.floorprice);
                $('#update_product_info_form input[name="size"]').val(data.size);
                $('#update_product_info_form input[name="primary_key"]').val(data.id);
                $('#update_product_info_form input[name="img_id"]').val(data.img_id);
                $('#update_product_info_form img').attr('src', data.url);
                $('#update_product_info_form input[name="status"][value="' + data.status + '"]').iCheck('check');
                $('#update_product_info_form input[name="is_home"][value="' + data.is_home + '"]').iCheck('check');
            }
        });
    });

    // Category
    $('#detailCategory').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/category/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#update_category_form input[name="name"]').val(data.name);
                $('#update_category_form input[name="description"]').val(data.description);
                $('#update_category_form input[name="alias"]').val(data.img.alias);
                if(data.img.from == '本地') {
                    $('#update_category_form input[name="url"]').val(data.img.url.substr(data.img.url.lastIndexOf('/') + 1));
                } else {
                    $('#update_category_form input[name="url"]').val(data.img.url);
                }
                $('#update_category_form input[name="primary_key"]').val(data.id);
                $('#update_category_form img').attr('src', data.img.url);
                $('#update_product_image_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });


    // Product
    $('#detailProduct').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/product/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#update_product_form input[name="name"]').val(data.name);
                $('#update_product_form input[name="summary"]').val(data.summary);
                $('#update_product_form input[name="price"]').val(data.price);
                $('#update_product_form input[name="cp_hot"]').val(data.cp_hot);
                $('#update_product_form input[name="member_price"]').val(data.member_price);
                $('#update_product_form input[name="primary_key"]').val(data.id);
                $('#update_product_form input[name="alias"]').val(data.img.alias);
                $('#update_product_form input[name="category"]').val(data.category.name);
                if(data.img.from == '本地') {
                    $('#update_product_form input[name="url"]').val(data.img.url.substr(data.img.url.lastIndexOf('/') + 1));
                } else {
                    $('#update_product_form input[name="url"]').val(data.img.url);
                }
                $('#update_product_form img').attr('src', data.img.url);
                $('#update_product_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // ThemeProduct
    $('#detailThemeProduct').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/theme_product/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                $('#theme_product_info').empty();
                var data = data.data;
                var rows = data.products;
                rows.forEach(function (item) {
                    var tr = '<tr>' +
                                '<td><small>' + data.name + '</small></td>\n' +
                                '<td><small>' + item.name + '</small></td>\n' +
                                '<td><button type="button" class="btn-danger btn-xs" data-toggle="deleteThemeProduct"  data-bid="' + item.id + '" data-tid="' + data.id + '"  title="删除此关联"><i class="fa fa-exclamation-circle"></i></button></td>\n' +
                             '</tr>';
                    $('#theme_product_info').append(tr);
                });
                $('button[data-toggle="deleteThemeProduct"]').on('click',function () {
                    var pid = $(this).data('bid');
                    var tid = $(this).data('tid');
                    swal({
                            title: "确定要删除此关联吗?",
                            text: "",
                            type: "warning",
                            showCancelButton: true,
                            confirmButtonColor: "#DD6B55",
                            confirmButtonText: "确定!",
                            cancelButtonText: "取消!",
                            closeOnConfirm: false,
                            closeOnCancel: false
                        },
                        function(isConfirm){
                            if (isConfirm) {
                                $.ajax({
                                    url : '/theme_product/delete',
                                    type : 'post',
                                    data : {pid:pid, tid:tid},
                                    success : function (data) {
                                        var swal_type = data.status == 1 ? 'success' : 'error';
                                        swal(data.message, '', swal_type);
                                        if(data.status == 1) {
                                            $('#detailThemeProduct').modal('hide');
                                        }
                                    }
                                })
                            } else {
                                swal("cancel", "您取消了此动作", "error");
                            }
                        });
                });
            }
        });
    });

    // Agent
    $('#detailAgent').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/agent/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#update_agent_form input[name="address"]').val(data.address);
                $('#update_agent_form input[name="invitation_code"]').val(data.invitation_code);
                $('#update_agent_form input[name="agent_balance"]').val(data.agent_balance);
                $('#update_agent_form input[name="description"]').val(data.description);
                $('#update_agent_form input[name="primary_key"]').val(data.id);
                $('#update_agent_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // Card
    $('#detailCard').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/bank_card/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                switch(data.type){
                    case 1:
                        $('#update_card_form select option[value="1"]').attr('selected', 'selected');
                        break;
                    case 2:
                        $('#update_card_form select option[value="2"]').attr('selected', 'selected');
                        break;
                    case 3:
                        $('#update_card_form select option[value="3"]').attr('selected', 'selected');
                        break;
                    case 4:
                        $('#update_card_form select option[value="4"]').attr('selected', 'selected');
                        break;
                    default:
                        break;
                }
                $('#update_card_form input[name="bank_name"]').val(data.bank_name);
                $('#update_card_form input[name="branch_name"]').val(data.branch_name);
                $('#update_card_form input[name="surename"]').val(data.surename);
                $('#update_card_form input[name="balance"]').val(data.balance);
                $('#update_card_form input[name="card_number"]').val(data.card_number);
                $('#update_card_form input[name="primary_key"]').val(data.id);
                $('#update_card_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // Message
    $('#detailMessage').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/message/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                // $('#update_product_form input[name="name"]').val(data.name);
                // $('#update_product_form input[name="summary"]').val(data.summary);
                // $('#update_product_form input[name="primary_key"]').val(data.id);
                // $('#update_product_form input[name="alias"]').val(data.img.alias);
                // $('#update_product_form input[name="category"]').val(data.category.name);
                // if(data.img.from == '本地') {
                //     $('#update_product_form input[name="url"]').val(data.img.url.substr(data.img.url.lastIndexOf('/') + 1));
                // } else {
                //     $('#update_product_form input[name="url"]').val(data.img.url);
                // }
                // $('#update_product_form img').attr('src', data.img.url);
                // $('#update_product_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // detailNotice
    $('#detailNotice').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/notice/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#update_notice_form input[name="contents"]').val(data.contents);
                $('#update_notice_form input[name="start_time"]').val(data.start_time);
                $('#update_notice_form input[name="end_time"]').val(data.end_time);
                $('#update_notice_form input[name="sort"]').val(data.sort);
                $('#update_notice_form input[name="primary_key"]').val(data.id);
                $('#update_notice_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // User
    $('#detailUser').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/user/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#update_user_form input[name="username"]').val(data.username);
                $('#update_user_form input[name="surename"]').val(data.surename);
                $('#update_user_form input[name="balance"]').val(data.balance);
                $('#update_user_form input[name="qq"]').val(data.qq);
                $('#update_user_form input[name="email"]').val(data.email);
                $('#update_user_form input[name="mobile"]').val(data.mobile);
                $('#update_user_form input[name="reg_ip"]').val(data.reg_ip);
                $('#update_user_form input[name="login_times"]').val(data.login_times);
                $('#update_user_form input[name="lastLogin_time"]').val(data.lastLogin_time);
                $('#update_user_form input[name="primary_key"]').val(data.id);
                $('#update_user_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // 去收货
    $('#deliver_goods').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/order/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                console.log(data);
                $('#deliver_goods_form input[name="billno"]').val(data.order_no);
                $('#deliver_goods_form input[name="username"]').val(data.user.username);
                $('#deliver_goods_form input[name="express_name"]').val(data.express_name);
                $('#deliver_goods_form input[name="express_price"]').val(data.express_price);
                $('#deliver_goods_form input[name="primary_key"]').val(data.id);
                $('#deliver_goods_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // 确认收货 show_info
    $('#collect_goods').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/order/detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                var data = data.data;
                $('#collect_goods_form input[name="billno"]').val(data.order_no);
                $('#collect_goods_form input[name="username"]').val(data.user.username);
                $('#collect_goods_form input[name="express_name"]').val(data.express_name);
                $('#collect_goods_form input[name="express_price"]').val(data.express_price);
                $('#collect_goods_form input[name="express_no"]').val(data.express_no);
                $('#collect_goods_form input[name="id"]').val(data.id);
                $('#collect_goods_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });

    // show_info 展示订单详情
    $('#show_info').on('show.bs.modal', function(e) {
        var id = $(e.relatedTarget).data('bid');
        $.ajax({
            url: '/order/product_detail',
            type: 'get',
            data: {id:id},
            success: function(data) {
                $('#show_info_tbody').empty();
                var data = data.data;
                console.log(data);
                $('#show_info_form input[name="billno"]').val(data.order_no);
                $('#show_info_form input[name="snap_address"]').val(data.snap_address.province+'-'+data.snap_address.city+'-'+data.snap_address.country+'-'+data.snap_address.detail);
                $('#show_info_form input[name="mobile"]').val(data.snap_address.mobile);
                // var real_price = data.real_price;
                for(var i = 0; i < data.product.length; i++) {
                    // var real_one_price = (data.product.is_vip == 1) ? data.product.
                    // var real_one_price = (data.product.is_vip == 1) ? data.product.
                    var tr = '<tr>' +
                        '<td>' + data.product[i].color + '</td>' +
                        '<td>' + data.product[i].size + '</td>' +
                        '<td>' + data.product[i].price + '</td>' +
                        '</tr>';
                    $('#show_info_tbody').append(tr);
                }
                // var tr = '<tr>' +
                //             '<td>1</td>' +
                //             '<td>Mark</td>' +
                //             '<td>Otto</td>' +
                //             '<td>@mdo</td>' +
                //          '</tr>';
                // $('#show_info_form input[name="username"]').val(data.user.username);
                // $('#show_info_form input[name="express_name"]').val(data.express_name);
                // $('#show_info_form input[name="express_price"]').val(data.express_price);
                // $('#show_info_form input[name="express_no"]').val(data.express_no);
                // $('#show_info_form input[name="id"]').val(data.id);
                // $('#show_info_form input[name="status"][value="' + data.status + '"]').iCheck('check');
            }
        });
    });
})(jQuery);

